import 'package:flutter/material.dart';
import 'package:my_enjoy/pages/home/index.dart';
import 'package:my_enjoy/pages/mine/index.dart';

class Tabbarpage extends StatefulWidget {
  const Tabbarpage({super.key});

  @override
  State<Tabbarpage> createState() => _TabbarpageState();
}

class _TabbarpageState extends State<Tabbarpage> {
  int _currentIndex = 0;
  // 底部TabBar数据
  List tabBarList = [
    {
      "title": "首页",
      "default": "assets/tabs/home_default.png",
      "active": "assets/tabs/home_active.png",
    },
    {
      "title": "我的",
      "default": "assets/tabs/my_default.png",
      "active": "assets/tabs/my_active.png",
    },
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: IndexedStack(
          index: _currentIndex,
          children: [HomePage(), MinePage()],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: barItemList(),
        currentIndex: _currentIndex,
        selectedItemColor: const Color.fromARGB(255, 85, 145, 175),
        unselectedItemColor: Colors.black,
        onTap: (index) {
          print(index);
          setState(() {
            _currentIndex = index;
          });
        },
      ),
    );
  }

  // 底部TabBar
  List<BottomNavigationBarItem> barItemList() {
    List<BottomNavigationBarItem> list = [];
    for (var i = 0; i < tabBarList.length; i++) {
      list.add(
        BottomNavigationBarItem(
          icon: Image.asset(tabBarList[i]['default'], width: 24),
          activeIcon: Image.asset(tabBarList[i]['active'], width: 24),
          label: tabBarList[i]['title'],
        ),
      );
    }
    return list;
  }
}
